module.exports = function ($http, $q, commonConfig,$rootScope,$localStorage) {
    return {
        getAccessToken:function(){   // 获取公共token
            var deferred = $q.defer();
            $http({
                method: 'POST',
                url:commonConfig.tokenApi+"oauth/token?grant_type=client_credentials&client_id="+commonConfig.client_id+"&client_secret="+commonConfig.client_secret
            }).then(function successCallback(response) {
                //业务处理
                deferred.resolve(response);
            }, function errorCallback(error) {
                //业务处理
                deferred.reject(error);
            });
            return deferred.promise;
        },
        checkAccessToken:function(token){   // 获取公共token
            var deferred = $q.defer();
            $http({
                method: 'GET',
                url:commonConfig.tokenApi + 'user?access_token='+token
            }).then(function successCallback(response) {
                //业务处理
                deferred.resolve(response);
            }, function errorCallback(error) {
                //业务处理
                deferred.reject(error);
            });
            return deferred.promise;
        },
        getLoginAccessToken:function(code,url){   // 获取登录token
            var deferred = $q.defer();
            $http({
                method: 'POST',
                url:commonConfig.tokenApi+"oauth/token?grant_type=authorization_code&code=" + code + "&redirect_uri="+ url +"&client_id="+commonConfig.client_id+"&client_secret=" + commonConfig.client_secret
            }).then(function successCallback(response) {
                //业务处理
                deferred.resolve(response);
            }, function errorCallback(error) {
                //业务处理
                deferred.reject(error);
            });
            return deferred.promise;
        },
        getCode:function(url){   // 获取Code
            console.log(url);
            var link = commonConfig.tokenApi+"oauth/authorize?client_id="+commonConfig.client_id+"&response_type=code&redirect_uri="+url;
            console.log(link);
            window.location.href = link;
        },
        getUser:function(token){
            var deferred = $q.defer();  //获取用户信息
            $http.get(commonConfig.tokenApi+'user?access_token=' + token).then(function successCallback(response) {
                //业务处理
                deferred.resolve(response);
            }, function errorCallback(error) {
                //业务处理
                deferred.reject(error);
            });
            return deferred.promise;
        },
        getLocalToken:function(){
            // console.log($localStorage.get('cloud'));
            return $localStorage.get('cloud')?$localStorage.get('cloud').token:null;
        },
        /*--------------------------------------------------------------*/
        Request: function (url, method, data){
            var pattern = /\?/g;
            pattern.test(url)?url+='&':url+='?';
            var _data = {
                method: method,
                url: commonConfig.$webroot+url+'access_token='+this.getLocalToken(),
                data: data,
                headers: {
                    "Accept": "*/*"
                }
            };
            var Result = $q.defer();
            $http(_data).success(function (res) {
                Result.resolve(res);
            }).error(function (err) {
                Result.reject(err);
            });
            return Result.promise;
        },
        eduRequest: function (url, method, data){
            var pattern = /\?/g;
            pattern.test(url)?url+='&':url+='?';
            var _data = {
                method: method,
                url: commonConfig.$webroot_2+url+'access_token='+this.getLocalToken(),
                data: data,
                headers: {
                    "Accept": "*/*"
                }
            };
            var Result = $q.defer();
            $http(_data).success(function (res) {
                Result.resolve(res);
            }).error(function (err) {
                Result.reject(err);
            });
            return Result.promise;
        }
    }
};